package com.ruoyi.web.controller.shop.base;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.RestaurantGoodsDetail;
import com.ruoyi.common.core.domain.entity.RestaurantStoreResource;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.RestaurantGoodsDetailService;
import com.ruoyi.system.service.RestaurantStoreResourceService;
import com.ruoyi.system.service.resp.RestaurantGoodsDetailDTO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: ruohao
 * @Date: 2023/9/1
 **/
@RestController
@RequestMapping("/shop/goods/detail")
public class BaseDetailController extends BaseController {
    @Resource
    private RestaurantGoodsDetailService restaurantGoodsDetailService;

    @Resource
    private RestaurantStoreResourceService restaurantStoreResourceService;

    //("@ss.hasPermi('store:resource:list')")
    @GetMapping("/list")
    public TableDataInfo list(RestaurantGoodsDetail restaurantGoodsDetail)
    {
        startPage();
        List<RestaurantGoodsDetail> list = restaurantGoodsDetailService.selectList(restaurantGoodsDetail);
        return getDataTable(list);
    }

    //("@ss.hasPermi('store:resource:list')")
    @GetMapping("/listAll")
    public TableDataInfo listAll(RestaurantGoodsDetail restaurantGoodsDetail)
    {
        List<RestaurantGoodsDetail> list = restaurantGoodsDetailService.selectList(restaurantGoodsDetail);
        return getDataTable(fillToDTO(list));
    }


    //("@ss.hasPermi('store:resource:remove')")
    @Log(title = "菜品管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(restaurantGoodsDetailService.deleteById(id));
    }

    //("@ss.hasPermi('store:resource:edit')")
    @Log(title = "菜品管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RestaurantGoodsDetail restaurantGoodsDetail) {
        restaurantGoodsDetail.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        Boolean result = restaurantGoodsDetailService.update(restaurantGoodsDetail);
        if (result) {
            return success();
        }
        return error();
    }

    //("@ss.hasPermi('store:resource:add')")
    @Log(title = "菜品管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody RestaurantGoodsDetail restaurantGoodsDetail)
    {
        restaurantGoodsDetail.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        Boolean result = restaurantGoodsDetailService.create(restaurantGoodsDetail);
        if (result) {
            return success();
        }
        return error();
    }

    private List<RestaurantGoodsDetailDTO> fillToDTO(List<RestaurantGoodsDetail> restaurantGoodsDetails) {
        if (CollectionUtils.isEmpty(restaurantGoodsDetails)) {
            return new ArrayList<>();
        }
        List<Long> resourceIds = restaurantGoodsDetails.stream().map(RestaurantGoodsDetail::getResourceId).collect(Collectors.toList());
        Map<Long, RestaurantStoreResource> restaurantStoreResourceMap = restaurantStoreResourceService.selectRestaurantStoreResourceByIds(resourceIds).stream().collect(Collectors.toMap(RestaurantStoreResource::getId, g -> g));
        return restaurantGoodsDetails.stream().map(g -> {
            RestaurantGoodsDetailDTO restaurantGoodsDetailDTO = new RestaurantGoodsDetailDTO();
            restaurantGoodsDetailDTO.setId(g.getId());
            restaurantGoodsDetailDTO.setGoodsId(g.getGoodsId());
            restaurantGoodsDetailDTO.setUnit(g.getUnit());
            restaurantGoodsDetailDTO.setAmount(g.getAmount());
            restaurantGoodsDetailDTO.setResourceId(g.getResourceId());
            restaurantGoodsDetailDTO.setCreateBy(g.getCreateBy());
            restaurantGoodsDetailDTO.setCreateTime(g.getCreateTime());
            restaurantGoodsDetailDTO.setUpdateBy(g.getUpdateBy());
            restaurantGoodsDetailDTO.setUpdateTime(g.getUpdateTime());

            RestaurantStoreResource restaurantStoreResource = restaurantStoreResourceMap.get(g.getResourceId());
            if (restaurantStoreResource != null) {
                restaurantGoodsDetailDTO.setResourceName(restaurantStoreResource.getName());
            }
            return restaurantGoodsDetailDTO;
        }).collect(Collectors.toList());
    }

    private RestaurantGoodsDetailDTO fillOneToDTO(RestaurantGoodsDetail restaurantGoodsDetail) {
        if (restaurantGoodsDetail == null) {
            return null;
        }
        RestaurantGoodsDetailDTO restaurantGoodsDetailDTO = new RestaurantGoodsDetailDTO();
        restaurantGoodsDetailDTO.setId(restaurantGoodsDetail.getId());
        restaurantGoodsDetailDTO.setGoodsId(restaurantGoodsDetail.getGoodsId());
        restaurantGoodsDetailDTO.setUnit(restaurantGoodsDetail.getUnit());
        restaurantGoodsDetailDTO.setAmount(restaurantGoodsDetail.getAmount());
        restaurantGoodsDetailDTO.setResourceId(restaurantGoodsDetail.getResourceId());
        restaurantGoodsDetailDTO.setCreateBy(restaurantGoodsDetail.getCreateBy());
        restaurantGoodsDetailDTO.setCreateTime(restaurantGoodsDetail.getCreateTime());
        restaurantGoodsDetailDTO.setUpdateBy(restaurantGoodsDetail.getUpdateBy());
        restaurantGoodsDetailDTO.setUpdateTime(restaurantGoodsDetail.getUpdateTime());

        RestaurantStoreResource restaurantStoreResource = restaurantStoreResourceService.selectRestaurantStoreResourceById(restaurantGoodsDetail.getId());
        if (restaurantStoreResource != null) {
            restaurantGoodsDetailDTO.setResourceName(restaurantStoreResource.getName());
        }
        return restaurantGoodsDetailDTO;
    }
}
